﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;

namespace YTSK.cSK
{
    class cDll
    {
        [DllImport("user32.dll")]
        public static extern IntPtr GetActiveWindow();

        [DllImport("User32.dll")]
        public static extern IntPtr WindowFromPoint(Point p);

        [DllImport("user32.dll")]
        public static extern IntPtr GetForegroundWindow();
        [DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32.dll")]
        public static extern bool SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

        [DllImport("user32.dll")]
        public static extern int GetWindowTextLength(IntPtr hwnd);
        //[DllImport("user32.dll")]
        //public static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString,int nMaxCount);
        [DllImport("user32.dll", EntryPoint = "GetWindowTextA", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(StringBuilder IpClassName, StringBuilder IpWindowName);
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, StringBuilder IpClassName, StringBuilder IpWindowName);

        /*该函数返回与指定窗口有特定关系*GW_CHILD = 5,GW_HWNDNEXT = 2 */
        [DllImport("user32.dll")]
        public static extern IntPtr GetWindow(IntPtr hWnd, int wCmd);

        [DllImport("user32.dll")]
        public static extern int GetClassName(IntPtr hwnd, StringBuilder IpClassName, int nMaxCount);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern int PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
        [DllImport("user32.dll", SetLastError = true)]
        public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);

        public delegate bool CallBack(IntPtr hwnd, int lParam);
        [DllImport("user32", CharSet = CharSet.Unicode)]
        public static extern int EnumWindows(CallBack x, int y);

        [DllImport("user32", CharSet = CharSet.Unicode)]
        public static extern int EnumChildWindows(IntPtr hWndParent, CallBack lpfn, int lParam);
        [DllImport("user32.dll")]
        public static extern int GetWindowRect(IntPtr hwnd, out System.Drawing.Rectangle lpRect);

        [DllImport("user32.dll")]
        public static extern bool IsWindow(IntPtr hWnd);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr GetParent(IntPtr hWnd);
        [DllImport("user32.dll", SetLastError = true)]
        public static extern void mouse_event(MouseEvent dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

        [DllImport("user32.dll")]
        // GetCursorPos() makes everything possible
        public static extern bool GetCursorPos(ref Point lpPoint);

        [DllImport("user32.dll")]
        public static extern int SetCursorPos(int x, int y);

        [DllImport("user32.dll", EntryPoint = "keybd_event")]
        /* byte bVk,    //虚拟键值
         *  byte bScan,// 一般为0
         *  int dwFlags,  //这里是整数类型  0 为按下，2为释放
         *  int dwExtraInfo  //这里是整数类型 一般情况下设成为 0
         */
        public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

        /// <summary>
        /// 判断窗口是否假死
        /// </summary>
        /// <param name="hwnd"></param>
        /// <returns></returns>
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern bool IsHungAppWindow(IntPtr hwnd);


        [DllImport("user32.dll")]
        public static extern IntPtr GetDC(IntPtr ptr);
        [DllImport("gdi32.dll")]
        public static extern int GetDeviceCaps(
        IntPtr hdc, // handle to DC
        DeviceCapsIndex nIndex // index of capability
            );
        /// <summary>
        /// 释放
        /// </summary>
        [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
        public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);


        [DllImport("gdi32.dll")]
        public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);

        [DllImport("User32.dll")]
        public static extern int GetSystemMetrics(int hWnd);
        

        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); //获取窗口句柄

        [DllImport("User32.dll", EntryPoint = "FindWindowEx")]
        public static extern IntPtr FindWindowEx(IntPtr hWndParent, IntPtr hWndChrild, string lpClassName, string
            lpWindowName); //获取子窗口句柄

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);

        [DllImport("user32")]
        public static extern bool GetClientRect(IntPtr hwnd, out RECT lpRect);

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT //定义坐标
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }
    }
    #region DeviceCaps常量
    public enum DeviceCapsIndex
    {
        HORZRES = 8,
        VERTRES = 10,
        LOGPIXELSX = 88,
        LOGPIXELSY = 90,
        DESKTOPVERTRES = 117,
        DESKTOPHORZRES = 118
    }
    #endregion
}
